<template>
  <el-col :gutter="40" class="panel-group" style="height:100%">
    <el-row :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
        <div class="card-panel-icon-wrapper icon-cpu">
          <svg-icon icon-class="cpu" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            CPU利用率
          </div>
          <!-- <count-to :start-val="0.00" :end-val="20.02" :duration="3000" class="card-panel-num" /> -->
          <div style="font-size: 18px">{{ GaugeData[0] }}</div>
        </div>
      </div>
    </el-row>
    <el-row :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('messages')">
        <div class="card-panel-icon-wrapper icon-disk">
          <svg-icon icon-class="disk" class-name="card-panel-icon" />
        </div>
        <el-tooltip class="item" effect="dark" :content="diskUsageTips" placement="top-end">
          <div class="card-panel-description">
            <div class="card-panel-text">
              磁盘剩余
            </div>
            <div style="font-size: 18px">{{ GaugeData[1] }}</div>
            <!-- <count-to :start-val="0" :end-val="81212" :duration="3000" class="card-panel-num" /> -->
          </div>
        </el-tooltip>
      </div>
    </el-row>
    <el-row :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('purchases')">
        <div class="card-panel-icon-wrapper icon-memory">
          <svg-icon icon-class="memory" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            内存利用率
          </div>
          <div style="font-size: 18px">{{ GaugeData[2] }}</div>
          <!-- <count-to :start-val="0" :end-val="9280" :duration="3200" class="card-panel-num" /> -->
        </div>
      </div>
    </el-row>
    <el-row :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('shoppings')">
        <div class="card-panel-icon-wrapper icon-internet">
          <svg-icon icon-class="internet" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            网络利用率
          </div>
          <div style="font-size: 18px">{{ GaugeData[3] }}</div>
          <!-- <count-to :start-val="0" :end-val="13600" :duration="3600" class="card-panel-num" /> -->
        </div>
      </div>
    </el-row>
  </el-col>
</template>

<script>
// import CountTo from 'vue-count-to'
// import axios from 'axios'
import axiosservice from '@/utils/CommonData.js'
export default {
  components: {
    // CountTo
  },
  data() {
    return {
      diskUsageTips: '总计49G,占用91.30%',
      timer: null,
      GaugeData: ['20.5%', '4.00G', '98.6%', '10.1%']
    }
  },
  mounted() {
    this.init()
    this.timer = setInterval(this.init, 2000)
    // this.timer = setInterval(() => { // setInterval可调用多次，返回的 ID 值可用作 clearInterval() 方法的参数
    //   console.log('run')
    // }, 5 * 1000)
  },
  beforeDestroy() {
    clearInterval(this.timer)
    this.timer = null
  },
  methods: {
    init() {
      this.showGauge()
    },
    showGauge() {
      axiosservice.get('/monitor')
        .then((res) => {
          // eslint-disable-next-line eqeqeq
          if (res.status == 200) {
            const data0 = parseFloat(res.data.ioUsage.replace('%', '')).toFixed(2)
            // const data1 = parseFloat(res.data.cpuUsage.replace('%', '')).toFixed(2)
            const data2 = parseFloat(res.data.memUsage.replace('%', '')).toFixed(2)
            const data3 = parseFloat(res.data.netUsage.replace('%', '')).toFixed(2)
            var diskUsage = JSON.parse(res.data.diskUsage)
            // console.log(diskUsage)
            const data1 = parseFloat(diskUsage['/idle'].replace('G', '')).toFixed(2)
            this.diskUsageTips = '总计' + diskUsage['/total'] + ',占用' + diskUsage['/percentage']
            // console.log(this.GaugeData)
            const temp = []
            temp.push(data0 + '%', data1 + 'G', data2 + '%', data3 + '%')
            this.GaugeData = temp
            // 以下方法可以修改值但不会重新渲染
            // this.GaugeData[0] = data0
          }
        })
        .catch((error) => {
          console.log(error)
          // this.$message.error('获取状态数据失败!')
        })
    },
    handleSetLineChartData(type) {
      // this.$emit('handleSetLineChartData', type)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row{
  height: 20%;
  margin: 10% 10% 0% 10%;
}
.panel-group {
  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    display: flex;
    align-items: center; /*定义body的元素垂直居中*/
    justify-content: center; /*定义body的里的元素水平居中*/
    height: 100%;
    padding: 1%;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-cpu {
        background: #40c9c6;
      }

      .icon-disk {
        background: #36a3f7;
      }

      .icon-memory {
        background: #f4516c;
      }

      .icon-internet {
        background: #34bfa3
      }
    }

    .icon-cpu {
      color: #40c9c6;
    }

    .icon-disk {
      color: #36a3f7;
    }

    .icon-memory {
      color: #f4516c;
    }

    .icon-internet {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 0 14px 0 14px;
      padding: 10px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 40px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 16px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 15px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px;
      float: none !important;
    }
  }
}
</style>
